<?php
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('bbq');

$this->breadcrumbs=array(
    'Pages'=>array('/Cms/admin/page/admin'),
    $model->page->title=>array('/Cms/admin/page/update', 'id'=>$model->page_id),
    'Revision#'.$model->id.' - Customize Page CSS and JavaScript',
);

$script = "
jQuery.duplicate = function(me){
    var parent = jQuery(me).parents('.row').first();
    var duplicated = parent.clone();
    jQuery.each(jQuery('input', duplicated), function(){
        jQuery(this).val('');
    });
    
    jQuery('a[class*=\"_trigger\"]', duplicated).text('Select File');
    jQuery('a[class*=\"_remove\"]', duplicated).remove();
    
    parent.after(duplicated);
    
    var container = jQuery(me).parents('.files').first();    
    
    return false;
}

jQuery.remove = function(me){
    var container = jQuery(me).parents('.files').first();
    
    if (jQuery('.row', container).length <= 1) {
        jQuery(me).siblings('input').val('')
            .next('a').html('Select File');
    }
    else
    {    
        jQuery(me).parents('.row').first().remove();
    }    
    
    return false;
}
";
$cs->registerScript(__CLASS__.'#Duplicate', $script, CClientScript::POS_READY);

$script = "
jQuery('.copy-js, .copy-css').click(function(){
    var url = jQuery(this).attr('href');
    var template_content_id = parseInt(jQuery('#PageRevision_template_content_id').val());
    var type = jQuery(this).hasClass('copy-js') ? 'js' : 'css';
    var data = {'type' : type, 'template_content_id' : template_content_id};
    jQuery.get(url, data, function(){
        window.location = window.location;
    });
    return false;
});
";
$cs->registerScript(__CLASS__.'#CopyFromTemplate', $script);

$script = "
jQuery.fn.updateContainer = function(){
    var data = {};
    jQuery('.connectedSortable').each(function(){
        var container = jQuery(this).attr('id').replace('list-container-', '');
        data[container] = [];
        var query = jQuery(this).sortable('serialize');
        if (query.length)
        {
            data[container] = jQuery.deparam.querystring(query).items;
        }
    });
    data = jQuery.param.querystring('', data);
    jQuery('#containers').val(data);
}
setTimeout(function(){jQuery.fn.updateContainer();}, 500);
jQuery('.sort-handle .empty').each(function(){
    jQuery(this).closest('li.sortable').remove();
});
jQuery('.connectedSortable .disabled').each(function(){
    jQuery(this).closest('li.sortable').addClass('disabled');
});
";
$cs->registerScript(__CLASS__.'#UpdateContainer', $script);

$script = "
jQuery.fn.get_page_widget_id = function(obj)
{
    if (jQuery(obj).size())
        return parseInt(jQuery(obj).closest('li.sortable').attr('id').replace('id-', ''));
    return 0;
}
jQuery('#content').delegate('.page_delete', 'click', function(){
    var page_widget_id = jQuery(this).closest('.sortable').attr('id').replace('items_', '');
    if (!confirm('Are you sure you want to remove ?'))
        return false;
    jQuery(this).closest('li.sortable').remove();
    jQuery.fn.updateContainer();
    return false;
});
jQuery('#content').delegate('.sub_page', 'click', function(){
    var page_widget_id = jQuery(this).closest('.sortable').attr('id').replace('items_', '');
    var url = jQuery.param.querystring(jQuery(this).attr('href'), {'page_widget_id' : page_widget_id});
    window.location = url;
    return false;
});
jQuery('#content').delegate('.add_widget', 'click', function(){
    var container = jQuery(this).closest('.page_tree').attr('id').replace('list-container-', '');
    var url = jQuery.param.querystring(jQuery(this).attr('href'), {'container' : container, 'page_id' : ".(int) $pageId.", 'revision_id' : ".(int) $revision."});
    window.location = url;
    return false;
});
jQuery('#content').delegate('.change_layout', 'click', function(){
    var page_widget_id = jQuery(this).closest('.sortable').attr('id').replace('items_', '');
    var url = jQuery.param.querystring(jQuery(this).attr('href'), {'page_widget_id' : page_widget_id});
    window.location = url;
    return false;
});
jQuery('#content').delegate('.duplicate', 'click', function(){
    var page_widget_id = jQuery(this).closest('.sortable').attr('id').replace('items_', '');
    var url = jQuery.param.querystring(jQuery(this).attr('href'), {'page_widget_id' : page_widget_id});
    jQuery.get(url, function(){
        window.location = window.location.href.replace(/\#.*/, '');
    });
    return false;
});
";
$cs->registerScript(__CLASS__.'#WidgetActions', $script, CClientScript::POS_READY);
if (Yii::app()->request->getQuery('layout', false) === false)
{
    $cs->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
    $cs->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
    $cs->registerCoreScript('bbq');
    $cs->registerScript('PopupLink', "
jQuery('#content').undelegate('.sub_page', 'click');
$('.container').delegate('.ui-tabs-panel h2 a, .page_tree .sub_page', 'click', function(){
    var href = $.param.querystring($(this).attr('href'), {layout: 'popup'});
    var is_edit_widget = $(this).hasClass('sub_page');
    if (is_edit_widget)
    {
        var page_widget_id = jQuery(this).closest('.sortable').attr('id').replace('items_', '');
        href = jQuery.param.querystring(href, {'page_widget_id' : page_widget_id});
    }
    $.fancybox({
        type: 'iframe',
        href: href,
        width: '100%',
        height: '100%',
        showCloseButton: true,
        hideOnOverlayClick: false,
        autoScale: false,
        titleShow: false,
        onStart: function(){\$('body').css({'overflow':'hidden'});},
        onClosed: function(){
            \$('body').css({'overflow':'auto'});
            if (is_edit_widget == false) window.location = window.location.href.replace(/\#.*/, '');
        }
    });
    return false;
});
");
}
$cs->registerScript(__CLASS__.'#DisableDelete', "
$('.disable-delete').each(function(){
    $(this).closest('.sortable').addClass('disable-delete');
});
");
?>
<h2>Customize Page</h2>

<div class="form wide">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'page-revision-form',
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php
$this->widget('zii.widgets.jui.CJuiTabs', array(
    'id'=>'page-tabs',
    'tabs'=>array(
        'Content Structure'=>$this->renderPartial('_structure',array(
            'model'=>$model, 
            'containers'=>$containers,
        ), true),
        'CSS and JavaScript'=>$this->renderPartial('_files',array(
            'model'=>$model, 
            'form'=>$form, 
        ), true),
        'Revision #'.$model->id=>$this->renderPartial('_log',array(
            'model'=>$model,
        ), true),
        'SEO'=>$this->renderPartial('_seo',array(
            'model'=>$model,
        ), true),
    ),
    'options'=>array(
        'cookie'=>array('expires'=>30),
    ),
));
?>
    <?php if (is_array($containers) && count($containers)):?>
    <div class="row buttons">
        <?php echo CHtml::hiddenField('containers', ''); ?>
        <?php echo CHtml::submitButton('Save'); ?>
    </div>
    <?php endif;?>

<?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">
$('#page-revision-form').submit(function(){
    if ($.trim($('#PageRevision_log').val())=='')
    {
        alert('You must enter a log message for the new revision');
        return false;    
    }
    return true;    
});
</script>
